package com.wk.service.impl;

import com.wk.entity.ManagerInfo;
import com.wk.exception.ForbiddenUserException;
import com.wk.mapper.ManagerInfoMapper;
import org.apache.shiro.authc.UnknownAccountException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.Collections;

@Service
public class ManagerInfoServiceImpl{

    @Resource
    private ManagerInfoMapper managerInfoMapper;

    /**
     * 通过名称查找用户
     * @param username
     * @return
     */
    public ManagerInfo findByUsername(String username)
    {
        ManagerInfo managerInfo = managerInfoMapper.findByUsername(username);
        if (managerInfo == null)
        {
            throw new UnknownAccountException();
        }
        if (!managerInfo.getState())
        {
            throw new ForbiddenUserException();
        }
        if(managerInfo.getPidsList() == null)
        {
            managerInfo.setPidsList(Collections.singletonList(0));
        }else if(managerInfo.getPidsList().size() == 0){
            managerInfo.getPidsList().add(0);
        }

        return managerInfo;
    }

}
